package com.digdes.calendar.service.impl;

import com.digdes.calendar.dao.EventDAO;
import com.digdes.calendar.gwt.client.dto.EventDTOImpl;
import com.digdes.calendar.model.Event;
import com.digdes.calendar.model.impl.EventImpl;
import com.digdes.calendar.service.EventParticipantService;
import com.digdes.calendar.service.EventService;
import com.digdes.calendar.service.PlaceService;
import com.digdes.calendar.service.UserService;

import java.util.*;

public class EventServiceImpl implements EventService {
    private EventDAO eventDAO;
    private PlaceService placeService ;
    private UserService userService;
    private EventParticipantService participantService;

    public void deleteEvent(Long id) {
        eventDAO.deleteEvent(id);
    }

    public void setEventDAO(EventDAO eventDAO) {
        this.eventDAO = eventDAO;
    }

    public void setPlaceService(PlaceService placeService) {
        this.placeService = placeService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public List<EventDTOImpl> getEventsByDate(Date date) {
        return makeEventDtoSet(eventDAO.getEvents(date));
    }

    public void saveEvent (EventDTOImpl eventDTO){
     Event event = new EventImpl(eventDTO);
     eventDAO.saveEvent(event);
     eventDTO.setId(event.getId());
    }

    public Set<EventDTOImpl> getEventsByPlaceAndDate(Long id,Date start,Date finish){
        return makeEventDtoSet((eventDAO.getEventsByPlaceAndDate(id,start,finish)));
    }
    public Set<EventDTOImpl> getEventsByUserAndDate(Long id,Date start,Date finish){
        return makeEventDtoSet((eventDAO.getEventsByUserAndDate(id,start,finish)));
    }


    public static Set<EventDTOImpl> makeEventDtoSet(Set<Event> events) {
        Set<EventDTOImpl> eventsDto = new HashSet<EventDTOImpl>();
        for (Object e : events) {
            Event event = (Event) e;
            eventsDto.add(makeEventDto(event));
        }
        return eventsDto;
    }
    public static List<EventDTOImpl> makeEventDtoSet(List<Event> events) {
        List<EventDTOImpl> eventsDto = new ArrayList<EventDTOImpl>();
        for (Object e : events) {
            Event event = (Event) e;
            eventsDto.add(makeEventDto(event));
        }
        return eventsDto;
    }



    public List<EventDTOImpl> getOldEventsByDate(Date date) {
        List<Event> events = eventDAO.getOldEvents(date);
        return makeEventDtoSet(events);
    }

    public static EventDTOImpl makeEventDto(Event event) {
        EventDTOImpl eventDto = new EventDTOImpl();
        eventDto.setId(event.getId());
        eventDto.setNameEvent(event.getNameEvent());
        eventDto.setDescription(event.getDescription());
        eventDto.setAllDay(event.getIsAllDay());
        eventDto.setNeedNotice(event.getIsNeedNotice());
        eventDto.setDateStart(event.getDateStart());
        eventDto.setDateFinish(event.getDateFinish());
        eventDto.setMyPlace(event.getMyPlace());
        if (event.getAuthor() != null)
            eventDto.setAuthor(UserServiceImpl.makeUserDto(event.getAuthor()));
        if (event.getParticipants() != null)
            eventDto.setParticipants(EventParticipantServiceImpl.makeEventParticipantDtoSet(event.getParticipants()));
        if (event.getPlace() != null)
            eventDto.setPlace(PlaceServiceImpl.makePlaceDto(event.getPlace()));
        // eventDto.setFiles(event.getFiles());
        return eventDto;
    }

    public List<EventDTOImpl> getEventsBetween(Date dStart, Date dFinish) {
        return makeEventDtoSet(eventDAO.getEventsBetween(dStart, dFinish));
    }

    public void setParticipantService(EventParticipantService participantService) {
        this.participantService = participantService;
    }
}
